<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="coinfaces">

        <title>用grep搜索Tab字符</title>

    <link rel="stylesheet" href="../../theme/css/style.css">
    <link rel="stylesheet" href="../../theme/css/pygments.css">



</head>

<body>
    <div class="content">
<h1>用grep搜索Tab字符</h1>    <p>
        under
            <a href="../../tags/awk/">awk</a>
            <a href="../../tags/bash/">bash</a>
            <a href="../../tags/grep/">grep</a>
            <a href="../../tags/pcre/">pcre</a>
            <a href="../../tags/regex/">regex</a>
    </p>
    <p>
        in <a href="../../categories/tech/">tech</a>
    </p>
    <p>Published: 2016-09-20</p>


    <p>用grep搜索文件中的tab字符不太容易，因为在Bash里不能直接输入<tt class="docutils literal">Tab</tt>，grep不认<tt class="docutils literal">\t</tt>，Stackoverflow上的用户给出了<a class="reference external" href="http://stackoverflow.com/questions/1825552/grep-a-tab-in-unix">多种回答</a>，思路大致有以下几种：</p>
<ul class="simple">
<li>解决输入<tt class="docutils literal">Tab</tt>的问题，又分为两种方案：<ul>
<li>Bash里输入<tt class="docutils literal"><span class="pre">Ctrl-V</span> Tab</tt>即可，在脚本里也可以这么用，至少在编辑器里输入<tt class="docutils literal">Tab</tt>更容易，只是可读性不好。</li>
<li>使用<a class="reference external" href="http://www.gnu.org/software/bash/manual/bashref.html#ANSI_002dC-Quoting">Bash的ANSI-C quoting</a>，注意要写成<tt class="docutils literal">grep <span class="pre">$'\t'</span></tt>，不能写成<tt class="docutils literal">grep <span class="pre">'$\t'</span></tt>。</li>
</ul>
</li>
</ul>
<ul class="simple">
<li>使grep识别<tt class="docutils literal">\t</tt>，用<tt class="docutils literal"><span class="pre">-P</span></tt>选项使用PCRE正则表达式，即<tt class="docutils literal">grep <span class="pre">-P</span> '\t'</tt>。</li>
<li>用awk代替grep，即<tt class="docutils literal">awk <span class="pre">'/\t/'</span></tt>，但搜索目录不如grep方便。</li>
</ul>
<p>git grep支持以上所有适用于grep的解决方案。</p>
<p>（完）</p>

    </div>
<nav class="nav sidebar">
    <div class="siteinfo">
        <div class="sitename"><a href="../..">coinfaces</a></div>
        <div class="tagline">Another Side of Life</div>
    </div>

        <a href="../../categories/">categories</a>
        <a href="../..">&larr; Home</a>
</nav><footer class="footer">
    <span>
        &copy; jpli &middot;
        under <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">CC-BY-SA 4.0</a>
    </span>
    |
    <span>
        Powered by:
        <a href="http://blog.getpelican.com/">Pelican</a>
    </span>
</footer></body>
</html>